package com.huatao.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.huatao.auth.application.convert.AuthRoleDTOConverter;
import com.huatao.auth.application.dto.AuthRoleDTO;
import com.huatao.auth.domain.bo.AuthRoleBO;
import com.huatao.auth.domain.service.AuthRoleDomainService;
import com.huatao.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    @PostMapping("addRole")
    public Result<Boolean> addRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController addRole: {}", JSON.toJSONString(authRoleDTO));
            }
                Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色标识不能为空");
                Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
                AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertRoleDTOToBo(authRoleDTO);
                return Result.ok(authRoleDomainService.addRole(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController addRole error: {}", e.getMessage(), e);
            return Result.fail("添加失败");
        }
    }

    @PostMapping("updateRole")
    public Result<Boolean> updateRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController updateRole: {}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertRoleDTOToBo(authRoleDTO);
            return Result.ok(authRoleDomainService.updateRole(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController updateRole error: {}", e.getMessage(), e);
            return Result.fail("更新失败");
        }
    }

    @PostMapping("deleteRole")
    public Result<Boolean> deleteRole(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController deleteRole: {}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertRoleDTOToBo(authRoleDTO);
            return Result.ok(authRoleDomainService.deleteRole(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController deleteRole error: {}", e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }

}
